'use strict'

define(['jquery', 'bootstrap', 'shiftform'], function($, validate) {
  
  return {
    
    init: function() {
      this.cancel();
    },
    
    cancel: function(){
      
      var that = this;
      
      $(document).on("shown.bs.modal", "#maintaining-cancel", function (e) {
        
        var valid = that.check(e);

        if (valid) {
          
          var loadNumbers = new Array();
          $('input[name="loadNumber"]:checked').each(function() {
            var loadNumber = $(this).val();
            loadNumbers.push(loadNumber);
          });
          $(e.currentTarget).find('input[name="loadNumbers"]').val(loadNumbers);
          
          var $form = $(e.currentTarget).find("form");                          //bind shiftform
          $form.shiftform();
        }
        
      });
      
    },
    
    check: function(event) {
      
      var count = $('input[name="loadNumber"]:checked').length;
      var $submit = $(event.currentTarget).find('button[type="submit"]');
      var $cancelText = $(event.currentTarget).find('.cancel-text');
      
      if (count > 0) {
        $submit.removeClass('disabled');
        $cancelText.text('是否取消派车？');
        return true;
      } else {
        $submit.addClass('disabled');
        $cancelText.text('至少需要选择一项调度指令！').css({'color': 'red'});;
        return false;
      }
      
    }

  }  
})